package com.mxchip.livestar.av.screen;


import android.content.Context;
import android.graphics.RectF;
import android.opengl.Matrix;

import com.mxchip.livestar.ui.call.AVDevice;

import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;

public class ScreenSettings {

    private final Context context;

    public ScreenSettings(Context context) {
        this.context = context;
    }

    private Screen mainScreen;

    private final Screen[] sideScreens = new Screen[4];

    private final Screen[] allScreens = new Screen[5];

    public Screen getMainScreen() {
        return mainScreen;
    }

    public void setMainScreen(Screen mainScreen) {
        this.mainScreen = mainScreen;
        allScreens[0] = mainScreen;
    }

    public Screen[] getSideScreens() {
        return sideScreens;
    }

    public void setSideScreens(Screen screen, int index) {

        if (index >= allScreens.length || index < 0) {
            return;
        }

        sideScreens[index] = screen;
        allScreens[index + 1] = screen;
    }

    public Screen[] getAllScreens() {
        return allScreens;
    }

    @Nullable
    public float[] getMatrixByScreen(Screen screen, int displayWidth, int displayHeight) {

        // Log.e("ScreenSettings", "getMatrixByScreen displayWidth " + displayWidth + ", displayHeight " + displayHeight);
        // Log.e("ScreenSettings", this.toString() + screen + " getMatrixByScreen displayWidth " + displayWidth + ", displayHeight " + displayHeight);

        float[] matrix = null;

        if (screen == null) {
            return null;
        }

        if (screen.equals(mainScreen)) {
            matrix = new float[16];
            Matrix.setIdentityM(matrix, 0);
            // Matrix.scaleM(matrix, 0, 0.9f, 0.9f, 0f);
            rotateShareScreen(screen, matrix);
            return matrix;
        }

        for (int i = 0; i < sideScreens.length; i++) {

            // Log.e("ScreenSettings", "getMatrixByScreen i " + i);
            // Log.e("ScreenSettings", "getMatrixByScreen screen == sideScreens[i] " + (screen == sideScreens[i]));

            if (screen.equals(sideScreens[i])) {

                //距离屏幕的最小距离
                int mMargin = dip2px(context, 2f);

                // 小视频的宽度
                float mThumbnailWidth = displayWidth / 4;
                // 小视频的高度
                float mThumbnailHeight = displayHeight / 4;

                RectF rectF = new RectF(displayWidth - mThumbnailWidth - mMargin,
                        displayHeight - mMargin,
                        displayWidth - mMargin,
                        displayHeight - mMargin - mThumbnailHeight);

                float scaleX = 1f / 4f;
                float scaleY = 1f / 4f;
                float ratioX = (rectF.left - .5f * (1 - scaleX) * displayWidth) / rectF.width();
                float ratioY = (rectF.top - .5f * (1 + scaleY) * displayHeight) / rectF.height();

                matrix = new float[16];

                switch (i) {
                    case 0:
                        Matrix.setIdentityM(matrix, 0);
                        Matrix.scaleM(matrix, 0, scaleX, scaleY, 0f);
                        Matrix.translateM(matrix, 0, ratioX * 2, -1.4916667f * 2, 0f);
                        break;
                    case 1:
                        Matrix.setIdentityM(matrix, 0);
                        Matrix.scaleM(matrix, 0, scaleX, scaleY, 0f);
                        Matrix.translateM(matrix, 0, ratioX * 2, -1, 0f);
                        break;
                    case 2:
                        Matrix.setIdentityM(matrix, 0);
                        Matrix.scaleM(matrix, 0, scaleX, scaleY, 0f);
                        Matrix.translateM(matrix, 0, ratioX * 2, 1, 0f);
                        break;
                    case 3:
                        Matrix.setIdentityM(matrix, 0);
                        Matrix.scaleM(matrix, 0, scaleX, scaleY, 0f);
                        Matrix.translateM(matrix, 0, ratioX * 2, 1.4916667f * 2, 0f);
                        break;
                }

                // Log.e("ScreenSettings", "getMatrixByScreen matrix " + Arrays.toString(matrix));

                break;
            }
        }

        return matrix;
    }

    private void rotateShareScreen(Screen screen, float[] matrix) {
        if (screen instanceof PreviewScreen) {
            if (((PreviewScreen) screen).type != PreviewScreen.Type.FrontCamera) {
                Matrix.rotateM(matrix, 0, 180, 0, 1, 0);
            }
        } else if (screen instanceof DecoderScreen) {
            if (!((DecoderScreen) screen).getVideoDevice().getDeviceName().equals(AVDevice.CAMERA_LOCAL_SMALL) &&
                    !((DecoderScreen) screen).getVideoDevice().getDeviceName().equals(AVDevice.CAMERA_LOCAL)
            ) {
                Matrix.rotateM(matrix, 0, 180, 0, 1, 0);
            }
        }
    }

    private int dip2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    public String toString() {
        return super.toString() + "{" +
                "mainScreen=" + mainScreen +
                ", sideScreens=" + Arrays.toString(sideScreens) +
                ", allScreens=" + Arrays.toString(allScreens) +
                '}';
    }

    public List<Screen> zOrderScreens() {
        return Arrays.asList(allScreens);
    }
}
